<?php

class teacher_Model_DbTable_Dbteacher extends Zend_Db_Table_Abstract
{
	public function addNewEdu($post){
		try{
			$db=$this->getAdapter();
			$db->beginTransaction();
			$db_global = new Application_Model_DbTable_DbGlobal();
			$data_edu = array(
					'ed_name'	=>	$post["ed_name"],
					'status'	=>	$post["status"]
			);
			$this->_name="tbl_education";
			$GetEdu_Id=$this->insert($data_edu);
			$db->commit();
			return $GetEdu_Id;
		}catch(Exception $e){
			$db->rollBack();
		}
	}
	public function selectteacher(){
		$db = new Application_Model_DbTable_DbGlobal();
		//$db = $this->getAdapter();
		$sql = "SELECT * From tbl_teacher ";
		return  $db->getGlobalDb($sql);
	}
	public function geteacherskill($ids){
		$db = $this->getAdapter();
		$sql = "SELECT 
				  skt.*,
				  s.* 
				FROM
				  tbl_teacher AS t,
				  tbl_skill_teacher AS skt,
				  tbl_skill AS s 
				WHERE t.teacher_id = $ids
				  AND skt.teacher_id = t.teacher_id 
				  AND skt.skill_id = s.skill_id 
  AND s.status = 1  ";
		$rows = $db->fetchAll($sql);
		return $rows;
	}
	public function geteachersubject($id){
		$db = $this->getAdapter();
		$sql = "SELECT tc.*,sub.subject_name
				FROM tbl_teacher AS t,  tbl_teaching AS tc, tbl_subject AS sub WHERE t.teacher_id =".$id."
				AND tc.teacher_id = t.teacher_id
				AND sub.subject_id = tc.subject_id
				AND tc.status = 1 ";
		$rows = $db->fetchAll($sql);
		return $rows;
	}
	public function geteacher($id){
		$db = $this->getAdapter();
		$sql = "SELECT * FROM tbl_teacher WHERE teacher_id =".$id." LIMIT 1";
		$rows = $db->fetchRow($sql);
		return $rows;
	}
	public function gesubject($id){
		$db = $this->getAdapter();
		$sql = "SELECT tc.*,s.subject_name FROM tbl_teacher AS t, tbl_teaching AS tc,tbl_subject AS s
				WHERE tc.teacher_id = ".$id."
				AND tc.subject_id = s.subject_id
				GROUP BY tc.subject_id ";
		$rows = $db->fetchRow($sql);
		return $rows;
	}
	function insertTeacher($data)
	{ 
		try{
			$db = $this->getAdapter();
			$db->beginTransaction();
			$db_global= new Application_Model_DbTable_DbGlobal();
			$session_user=new Zend_Session_Namespace('auth');
			$userName = $session_user->user_name;
			$GetUserId= $session_user->user_id;
			$photoname = str_replace(" ", "_", $data['teacher_name']) . '.jpg';
			$upload = new Zend_File_Transfer();
			$upload->addFilter('Rename',
					array('target' => PUBLIC_PATH . '/images/fi-upload/'. $photoname, 'overwrite' => true) ,'photo');
			$receive = $upload->receive();
			
			if($receive)
			{
				$data['photo'] = $photoname;
			}
			else{
				$data['photo']="";
			}
			if(empty($data['unit_sale_price'])){
				$data['unit_sale_price']=0;
			}
			unset($data['MAX_FILE_SIZE']);
	     	$array_data = array(
	     			"id_code"	=>	$data["id_code"],
	     			"teacher_name"	=>	$data["teacher_name"],
	     			"gender"	=>	$data["gender"],
	     			"pob"	=>	$data["pob"],
	     			"dob"	=>	$data["dob"],
	     			"tel" =>$data["tel"],
	     			"email" =>$data["email"],
	     			"address" =>$data["address"],
	     			"password" =>md5($data["password"]),
	     			//"education_level" =>$data["education_level"],
	     			//"finish_from" =>$data["finish_from"],
	     			//"year" =>$data["year"],
	     			"description" =>$data["description"],
	     			"photo" =>$data["photo"],
	     			"status" =>$data["status"]
	     	);
	    $this->_name='tbl_teacher';
     	$tblteacher = $this->insert($array_data);	
     		$ids = explode(',',$data['identity']);
     		foreach ($ids as $i)
     		{
     			//$db->getProfiler()->setEnabled(true);
     			$data_teaching = array(
     			'teacher_id'	 	 => $tblteacher,
     			'subject_id'		 => $data["item_id_".$i],
     			"status" =>1
     			);
     			$this->_name='tbl_teaching';
     			$tbldata = $this->insert($data_teaching);
     		}
     		$idsk = explode(',',$data['identitysk']);
     		foreach ($idsk as $iskill)
     		{
     			$data_skill = array(
     					'teacher_id'	 	 => $tblteacher,
     					'skill_id'		 => $data["skill_id_".$iskill],
     					"status" =>1
     			);
     			$this->_name='tbl_skill_teacher';
     			$tblskill = $this->insert($data_skill);
      		}   
      			

      		$id_edu = explode(',',$data['identity_edu']);
      		foreach ($id_edu as $edu)
      		{
      			$data_edu = array(
      					't_id'	 	 		 => $tblteacher,
      					'edu_id'		 	 => $data["edu_id_".$edu],
      					'finish_from'	 	 => $data["finish_from_".$edu],
      					'year'		 		 => $data["year_".$edu],
      					"status" 			 =>1
      			);
      			$this->_name='tbl_teacher_edu';
      			$tbledu = $this->insert($data_edu);
      		}
     	$db->commit();
		}catch (Exception $e)
		{
			$db->rollBack();
			$e->getMessage();
		}
	}
	function updateTeacher($data)
	{
		try{
			$db = $this->getAdapter();
			$db->beginTransaction();
			$db_global= new Application_Model_DbTable_DbGlobal();
			$session_user=new Zend_Session_Namespace('auth');
			$userName = $session_user->user_name;
			$GetUserId= $session_user->user_id;
			$photoname = str_replace(" ", "_", $data['teacher_name']) . '.jpg';
			$upload = new Zend_File_Transfer();
			$upload->addFilter('Rename',
					array('target' => PUBLIC_PATH . '/images/fi-upload/'. $photoname, 'overwrite' => true) ,'photo');
			$receive = $upload->receive();
			if($receive)
			{
				$data['photo'] = $photoname;
			}
			else{
				$data['photo']="";
			}
			if(empty($data['unit_sale_price'])){
				$data['unit_sale_price']=0;
			}
			unset($data['MAX_FILE_SIZE']);
			$array_data = array(
					"id_code"			=>	$data["id_code"],
	     			"teacher_name"		=>	$data["teacher_name"],
	     			"gender"			=>	$data["gender"],
	     			"pob"				=>	$data["pob"],
	     			"dob"				=>	$data["dob"],
	     			"tel" 				=>	$data["tel"],
	     			"email" 			=>	$data["email"],
	     			"address" 			=>	$data["address"],
	     			//"education_level" 	=>	$data["education_level"],
	     			//"finish_from" 		=>	$data["finish_from"],
	     			//"year" 				=>	$data["year"],
	     			"description" 		=>	$data["description"],
	     			"photo" 			=>	$data["photo"],
	     			"status" 			=>	$data["status"]
			);
			$this->_name='tbl_teacher';
			$where = $this->getAdapter()->quoteInto('teacher_id=?', $data["teacher_id"]);
			$tblteacher = $this->update($array_data, $where);
			//unset($array_data);
			$sql = "Delete From tbl_teaching Where teacher_id = ".$data["teacher_id"];
			$db->query($sql);
			
			$sqls = "Delete From tbl_skill_teacher Where teacher_id = ".$data["teacher_id"];
			$db->query($sqls);
			
			$sql_edu = "Delete From tbl_teacher_edu Where t_id = ".$data["teacher_id"];
			$db->query($sql_edu);
			
			$ids = explode(',',$data['identity']);
			//print_r($ids);exit();
			if($ids !=""){
				foreach ($ids as $i)
				{
					$data_teaching = array(
							'teacher_id'	 	 => $data["teacher_id"],
							'subject_id'		 => $data["item_id_".$i],
							"status" 			 => 1
					);
					$this->_name='tbl_teaching';
     				$tbldata = $this->insert($data_teaching);
				}
			}
			$idskill = explode(',',$data['identitysk']);
			if($idskill !=""){
				foreach ($idskill as $iskill)
				{
					$data_skill = array(
							'teacher_id'	 	 => $data["teacher_id"],
							'skill_id'		 => $data["skill_id_".$iskill],
							"status" =>1
					);
					$this->_name='tbl_skill_teacher';
					$tbldata = $this->insert($data_skill);
				}
			}
			$id_edu = explode(',',$data['identity_edu']);
			print_r($id_edu);
			foreach ($id_edu as $edu)
			{
				$data_edu = array(
						't_id'	 	 		 => $data["teacher_id"],
						'edu_id'		 	 => $data["edu_id_".$edu],
						'finish_from'	 	 => $data["finish_from_".$edu],
						'year'		 		 => $data["year_".$edu],
						"status" 			 =>1
				);
				$this->_name='tbl_teacher_edu';
				$tbledu = $this->insert($data_edu);
			}
			$db->commit();
		}catch (Exception $e)
		{
			$db->rollBack();
			$e->getMessage();
		}
	}
	function udateTeacher($data)
	{
		try{
			$db = $this->getAdapter();
			$db->beginTransaction();
			$db_global= new Application_Model_DbTable_DbGlobal();
			$session_user=new Zend_Session_Namespace('auth');
			$userName = $session_user->user_name;
			$GetUserId= $session_user->user_id;
			//		$db->getProfiler()->setEnabled(true);
			$array_data = array(
					"id_code"	=>	$data["id_code"],
					"teacher_name"	=>	$data["teacher_name"],
					"gender"	=>	$data["gender"],
					"pob"	=>	$data["pob"],
					"address" => $data["address"],
					"dob"	=>	$data["dob"],
					"tel" =>$data["tel"],
					"email" =>$data["email"],
					"password" =>$data["password"],
					"education_level" =>$data["education_level"],
					"finish_from" =>$data["finish_from"],
					"year" =>$data["year"],
					"description" =>$data["description"],
					"status" =>$data["status"],
					"photo" =>$data[""]
			);
			$this->_name='tbl_teacher';
			$tblteacher = $this->insert($array_data);
			unset($array_data);
	
			//      	Zend_Debug::dump($db->getProfiler()->getLastQueryProfile()->getQuery());
			//      	Zend_Debug::dump($db->getProfiler()->getLastQueryProfile()->getQueryParams());
			//      	$db->getProfiler()->setEnabled(false);
	
	
			$ids = explode(',',$data['identity']);
			print_r($ids);//exit();
			foreach ($ids as $i)
			{
				print_r($i);
				//     			$db->getProfiler()->setEnabled(true);
				$data_teaching = array(
						'teacher_id'	 	 => $tblteacher,
						'subject_id'		 => $data["item_id_".$i],
				);
				$this->_name='tbl_teaching';
				$tbldata = $this->insert($data_teaching);
				//      			Zend_Debug::dump($db->getProfiler()->getLastQueryProfile()->getQuery());
				//      			Zend_Debug::dump($db->getProfiler()->getLastQueryProfile()->getQueryParams());
				//      			$db->getProfiler()->setEnabled(false);
			}
			unset($array_data);
			$ids = explode(',',$data['identitysk']);
			print_r($ids);//exit();
			foreach ($ids as $i)
			{
				print_r($i);
				//$db->getProfiler()->setEnabled(true);
				$data_teaching = array(
						'teacher_id'	 	 => $tblteacher,
						'skill_id'		 => $data["skill_id_".$i],
				);
				$this->_name='tbl_skill_teacher';
				$tbldata = $this->insert($data_teaching);
				//      			Zend_Debug::dump($db->getProfiler()->getLastQueryProfile()->getQuery());
				//      			Zend_Debug::dump($db->getProfiler()->getLastQueryProfile()->getQueryParams());
				//      			$db->getProfiler()->setEnabled(false);
			}
			$db->commit();
		}catch (Exception $e)
		{
			$db->rollBack();
			$e->getMessage();
		}
		//      	Zend_Debug::dump($db->getProfiler()->getLastQueryProfile()->getQuery());
		//      	Zend_Debug::dump($db->getProfiler()->getLastQueryProfile()->getQueryParams());
		//      	$db->getProfiler()->setEnabled(false);
	}
	public function addskill($arr)
	{
		$this->_name='tbl_skill';
		//print_r($arr);exit();
		$array_data = array(
				"skill"	=>	$arr["skills"],
				"status"	=>	$arr["skill_status"]
		);
		return $this->insert($array_data);	
	}
	public function updateStatus($arr)
	{
		$array_data = array(
				"teacher_id"	=>	$arr["teacher_id"],
				"status"	=>	1
		);
		$where = $this->getAdapter()->quoteInto('teacher_id=?', $arr["teacher_id"]);
		$this->_name='tbl_teacher';
		$this->update($array_data, $where);
			
	}
	public function updateUnStatus($arr)
	{
		$array_data = array(
				"teacher_id"	=>	$arr["teacher_id"],
				"status"	=>	0
		);
		$where = $this->getAdapter()->quoteInto('teacher_id=?', $arr["teacher_id"]);
		$this->_name='tbl_teacher';
		$this->update($array_data, $where);
			
	}
}

